package jp.co.ricoh.melody.views.fragments;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.presenters.ISoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.presenters.SoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.views.adapters.SoundTimeView;

/**
 * Created by tanyajie on 2016/11/16.
 */

public class SoundTimeListDialogFragment extends DialogFragment implements View.OnClickListener {
    /**
     * This button is used to select sound for melody and dismiss dialog.
     */
    private Button mOkBtn;
    /**
     * This button is used to dismiss dialog.
     */
    private Button mCancelBtn;

    private ListView mSoundTimeListView;

    private static String mSoundTime;

    private ArrayList<String> mList = new ArrayList<String>();
    private SoundTimeView mSoundTimeAdapter;

    /**
     * Presenter of this view.
     */
    private ISoundListDialogFragmentPresenter mISoundListDialogFragmentPresenter;
    private OnSoundTimeDialogDismissListener mOnSoundTimeDialogDismissListener;
    public SoundTimeListDialogFragment() {
    }

    public interface OnSoundTimeDialogDismissListener {
        void onDismiss(String soundTime);
    }

    public void setOnSoundTimeDialogDismissListener(OnSoundTimeDialogDismissListener listener) {
        mOnSoundTimeDialogDismissListener = listener;
    }

    public static SoundTimeListDialogFragment newInstance(String soundTime) {
        SoundTimeListDialogFragment soundTimeListDialogFragment = new SoundTimeListDialogFragment();
        mSoundTime = soundTime;
        return soundTimeListDialogFragment;
    }

    public void setOnDismissListener(SoundListDialogFragment.OnSoundDialogDismissListener onSoundDialogDismissListener) {
        mISoundListDialogFragmentPresenter = new SoundListDialogFragmentPresenter();
        mISoundListDialogFragmentPresenter.onCreate(getActivity(), onSoundDialogDismissListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_sound_time, container, false);
        initView(view);
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout(getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_width),
                    getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_height));
        }
    }

    /**
     * This method is used to initialize view.
     *
     * @param view
     */
    private void initView(View view) {
        mOkBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_ok);
        mOkBtn.setOnClickListener(this);
        mCancelBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_cancel);
        mCancelBtn.setOnClickListener(this);
        mSoundTimeListView = (ListView) view.findViewById(R.id.fragment_dialog_sound_time_list);

        initDataList();
        mSoundTimeAdapter =  new SoundTimeView(mList, getActivity(), mSoundTime);
        mSoundTimeListView.setAdapter(mSoundTimeAdapter);
        mSoundTimeListView.setSelection(searchSelectListItem(mSoundTime));

    }


    private void initDataList() {
        mList.add("10 sec");
        mList.add("15 sec");
        mList.add("30 sec");
        mList.add("60 sec");
        mList.add("120 sec");

        return ;
    }

    private int searchSelectListItem(String time) {
        for(int i = 0; i < mList.size(); i++) {
            if(time.equals(mList.get(i))) {
                return i;
            }
        }
        return 0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_dialog_sound_btn_ok:
                if (null != mOnSoundTimeDialogDismissListener) {
                    mSoundTime = mList.get(mSoundTimeAdapter.getSelectedItemPosition());
                    mOnSoundTimeDialogDismissListener.onDismiss(mSoundTime);
                }
                dismissAllowingStateLoss();
                break;
            case R.id.fragment_dialog_sound_btn_cancel:
                dismissAllowingStateLoss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }



}
